package com.wavefront;

import java.util.ArrayList;
import java.util.Hashtable;

import android.util.Log;

import com.wavefront.mtl.elements.Material;
import com.wavefront.obj.elements.Mesh;
import com.wavefront.obj.elements.VertexG;
import com.wavefront.obj.elements.VertexN;
import com.wavefront.obj.elements.VertexUV;
import com.wavefront.utils.ObjLoader;

public class Model {

	private Hashtable<String, Material> materials = new Hashtable<String, Material>();
	private ArrayList<VertexG> vertices = new ArrayList<VertexG>();
	private ArrayList<VertexN> normals = new ArrayList<VertexN>();
	private ArrayList<VertexUV> uv = new ArrayList<VertexUV>();
	private Hashtable<String, Mesh> meshes = new Hashtable<String, Mesh>();

	public Hashtable<String, Mesh> load(String fileName) {
		ObjLoader.load(fileName, this);
		return meshes;
	}

	public Hashtable<String, Material> getMaterials() {
		return materials;
	}

	public Hashtable<String, Mesh> getMeshes() {
		return meshes;
	}

	public void setMaterials(Hashtable<String, Material> materials) {
		this.materials = materials;
	}

	public void setMeshes(Hashtable<String, Mesh> meshes) {
		this.meshes = meshes;
	}

	public Mesh createNewMesh(String name) {
		Mesh newMesh = new Mesh(name);
		this.meshes.put(name, newMesh);
		return newMesh;
	}

	public VertexG createVertexG() {
		VertexG vertex = new VertexG();
		vertices.add(vertex);
		return vertex;
	}

	public VertexG createVertexG(float x, float y, float z) {
		VertexG vertex = new VertexG();
		vertex.setX(x);
		vertex.setY(y);
		vertex.setZ(z);
		vertices.add(vertex);
		return vertex;
	}

	public VertexUV createVertexUV() {
		VertexUV vertex = new VertexUV();
		uv.add(vertex);
		return vertex;
	}

	public VertexUV createVertexUV(float u, float v, float w) {
		VertexUV vertex = new VertexUV();
		vertex.setU(u);
		vertex.setV(v);
		vertex.setW(w);
		uv.add(vertex);
		return vertex;
	}

	public VertexN createVertexN() {
		VertexN normal = new VertexN();
		normals.add(normal);
		return normal;
	}

	public VertexN createVertexN(float i, float j, float k) {
		VertexN normal = new VertexN();
		normal.setI(i);
		normal.setJ(j);
		normal.setK(k);
		normals.add(normal);
		return normal;
	}

	public Material getMaterial(String materialName) {
		return materials.get(materialName);
	}

	public void printStatistic() {

		Log.i("model.info", "meshes:" + String.valueOf(meshes.size()));
		Log.i("model.info", "vertices:" + String.valueOf(vertices.size()));
		Log.i("model.info", "normals:" + String.valueOf(normals.size()));
		Log.i("model.info", "uv:" + String.valueOf(uv.size()));
		Log.i("model.info", "materials:" + String.valueOf(materials.size()));
	}

	public Material createMaterial(String materialName) {
		Material material = new Material(materialName);
		materials.put(materialName, material);
		return material;
	}
}
